package com.itheima.bos.web.action.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.itheima.bos.bean.base.Area;
import com.itheima.bos.service.AreaService;
import com.itheima.bos.utils.PinYin4jUtils;

@Actions
@Controller
@Namespace("/area")
@Scope("prototype")
@ParentPackage("json-default")
public class AreaAction extends BaseAction<Area> {

	private File file;
	private String fileFileName;

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public void setFile(File file) {
		this.file = file;
	}

	@Autowired
	private AreaService service;

	@Action(value = "area_batchImport")
	public String importArea() throws FileNotFoundException, IOException {
		List<Area> list = new ArrayList<Area>();
		Workbook workbook = null;
		if (fileFileName.endsWith("xls")) {
			workbook = new HSSFWorkbook(new FileInputStream(file));
		} else if (fileFileName.endsWith("xlsx")) {
			workbook = new XSSFWorkbook(new FileInputStream(file));
		}
		Sheet sheet = workbook.getSheetAt(0);
		for (Row row : sheet) {
			if (row.getRowNum() == 0) {
				continue;
			}
			if (row.getCell(0) == null || StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
				continue;
			}
			Area area = new Area();
			area.setId(row.getCell(0).getStringCellValue());
			area.setProvince(row.getCell(1).getStringCellValue());
			area.setCity(row.getCell(2).getStringCellValue());
			area.setDistrict(row.getCell(3).getStringCellValue());
			area.setPostcode(row.getCell(4).getStringCellValue());

			String province = area.getProvince();
			String city = area.getCity();
			String district = area.getDistrict();
			province = province.substring(0, province.length() - 1);
			city = city.substring(0, city.length() - 1);
			district = district.substring(0, district.length() - 1);
			String[] headArray = PinYin4jUtils.getHeadByString(province + city + district);
			StringBuffer buffer = new StringBuffer();
			for (String str : headArray) {
				buffer.append(str);
			}
			area.setShortcode(buffer.toString());

			String toPinyin = PinYin4jUtils.hanziToPinyin(city, "");
			area.setCitycode(toPinyin);
			list.add(area);
		}

		service.saveAreas(list);

		return NONE;
	}


	@Action(value = "area_pageQuery", results = { @Result(name = "success", type = "json") })
	public String area_pageQuery() {

		Pageable pageable = new PageRequest(page-1, rows);
		Specification<Area> specification = new Specification<Area>() {
			
			@Override
			public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate = null;
				List<Predicate> list = new ArrayList<Predicate>();
				
				if (StringUtils.isNoneBlank(model.getProvince())) {
					Predicate p = cb.like(root.get("province").as(String.class), "%" + model.getProvince() + "%");
					list.add(p);
				}
				if (StringUtils.isNoneBlank(model.getCity())) {
					Predicate p = cb.like(root.get("city").as(String.class), "%" + model.getCity() + "%");
					list.add(p);
				}
				if (StringUtils.isNoneBlank(model.getDistrict())) {
					Predicate p = cb.like(root.get("district").as(String.class), "%" + model.getDistrict() + "%");
					list.add(p);
				}
				if (list.size()>0 && list != null) {
					Predicate[] pres = new Predicate[list.size()];
					predicate = cb.and(list.toArray(pres));
				}
				
				return predicate;
			}
		};
		Page<Area> pageData = service.findPageData(specification, pageable);
		pushPageDataToValueStack(pageData);

		return SUCCESS;
	}

}
